# !/usr/bin python
#--*-- coding:utf-8 --*--
import os
import sys
import re

def get_info(line, reg):
    v = re.findall(reg, line)
    if len(v) > 0:
        return 1
    else:
        return 0



if __name__ == "__main__":
    #[2016/9/22 18:35:44.342]	Loc hal_gps_info,lat=6c2ae2 lon=1458f13 dwTimeStamp=1158575760 heading=357.539062 satnum=8 old-satnum=8
    #[2016/9/22 18:34:14.342]	  2016-9-22 18:34:30, coor.latitude = 38.025773, coor.longitude = 114.451897, loc.speed = 0.000000, loc.satellites = 8, loc.dirang = 356.132812, loc.resend = 0, sn=12345678
    file = open("slptt_188.log")

    points = []
    speed = []
    heading = []
    count = 0

    while 1:
        lines = file.readlines(100000)
        if not lines:
            break

        # for line in lines:
        #     reg = 'coor.latitude(.*?)'
        #     ret = get_info(line, reg) # do something
        #     if ret == 1:
        #         # 提取经纬度
        #         l = line.replace(' ', '').split(',')
        #         if len(l) > 0:
        #             v = l[3][10:]
        #             p = (l[2][15:], l[1][14:])
        #             s = l[4][15:]
        #             h = l[5][11:]
        #
        #             speed.append(v)
        #             points.append(p)
        #             heading.append(h)
        for line in lines:
            reg = 'Loc hal_gps_info(.*?)'
            ret = get_info(line, reg) # do something
            if ret == 1:
                # 提取经纬度
                l = line.split(',')
                l = l[1].split(' ')
                if len(l) > 0:
                    v = '5'#l[3][10:]
                    x = '0x'+l[0][4:]
                    x = eval(x)*360.0/pow(2,26)
                    y = '0x'+ l[1][4:]
                    y = eval(y)*360.0/pow(2,26)
                    p = (str(y), str(x))
                    s = l[4][7:]
                    h = l[3][8:]
                    print p
                    speed.append(v)
                    points.append(p)
                    heading.append(h)

    print len(points)

    #2 根据记录的经纬度信息生成js数据文件，用于地图显示，格式:
    #var data = {"data":[list],"total":5365,"rt_loc_cnt":47764510,"errorno":0,"NearestTime":"2014-08-29 15:20:00","userTime":"2014-08-29 15:32:11"}
    header = 'var data = {\"data\":['
    content = header
    
    for p in points:
        data = '[' + p[0] + ', ' + p[1] + ']'
        content = content + data + ','

    content = content + '],\"heading\":['

    # heading
    for h in heading:
        content = content + h + ','

    content = content + '],\"speed\":['

    # speed info
    speed = speed
    for s in speed:
        content = content + s + ','

    content = content + '],\"total\": ' + str(len(points)) + '}'

    with open('points-data.js', 'w') as fp:
        fp.write(content)
